package com.huawei.hms.framework.network.restclient.hwhttp.okhttp;

import android.os.SystemClock;
import com.huawei.hms.framework.common.Logger;
import com.huawei.hms.framework.network.restclient.hianalytics.EditableMetrics;
import com.huawei.hms.framework.network.restclient.hianalytics.RequestFinishedInfo;
import com.huawei.hms.framework.network.restclient.hwhttp.dns.DNManager;
import com.huawei.hms.framework.network.restclient.hwhttp.model.ConnectionInfo;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.List;
import java.util.WeakHashMap;
import java.util.concurrent.atomic.AtomicLong;
import javax.annotation.Nullable;
import o.ftz;
import o.fuh;
import o.fuk;
import o.fum;
import o.fum.a;
import o.fus;
import o.fut;
import o.fvb;
import okhttp3.Route;
import okhttp3.internal.connection.RealConnection;

public class HttpEventListener extends fum {
    private static final String TAG = "HttpEventListener";
    private static HttpEventListenerFactory factory = new HttpEventListenerFactory();
    private static AtomicLong nextCallId = new AtomicLong(1);
    private long callId = nextCallId.getAndIncrement();
    private long callStartTime;
    private ConnectionInfo connectionInfo;
    private OkhttpConnRequestFinishedInfo requestFinishedInfo = new OkhttpConnRequestFinishedInfo();
    private int retryTime = 0;

    public static class HttpEventListenerFactory implements a {
        private WeakHashMap<ftz, WeakReference<HttpEventListener>> events = new WeakHashMap();

        public HttpEventListener create(ftz o_ftz) {
            HttpEventListener httpEventListener = new HttpEventListener();
            this.events.put(o_ftz, new WeakReference(httpEventListener));
            return httpEventListener;
        }

        public HttpEventListener getListener(ftz o_ftz) {
            WeakReference weakReference = (WeakReference) this.events.get(o_ftz);
            if (weakReference != null) {
                return (HttpEventListener) weakReference.get();
            }
            return null;
        }
    }

    public static HttpEventListenerFactory getFactory() {
        return factory;
    }

    private void recordEventLog(String str, long j) {
        Logger.v(TAG, "callId = %d / %s : ElapsedTime = %d", Long.valueOf(this.callId), str, Long.valueOf(j - this.callStartTime));
    }

    public void callStart(ftz o_ftz) {
        super.callStart(o_ftz);
        this.requestFinishedInfo.getMetricsRealTime().setCallStartTime();
        this.requestFinishedInfo.getMetricsTime().setCallStartTime();
        this.requestFinishedInfo.setUrl(o_ftz.request().ˏ().toString());
        this.callStartTime = SystemClock.elapsedRealtime();
        recordEventLog("callStart", this.requestFinishedInfo.getMetricsRealTime().getCallStartTime());
    }

    public void dnsStart(ftz o_ftz, String str) {
        super.dnsStart(o_ftz, str);
        this.requestFinishedInfo.getMetricsRealTime().setDnsStartTime();
        this.requestFinishedInfo.getMetricsTime().setDnsStartTime();
        recordEventLog("dnsStart", this.requestFinishedInfo.getMetricsRealTime().getDnsStartTime());
    }

    public void dnsEnd(ftz o_ftz, String str, List<InetAddress> list) {
        super.dnsEnd(o_ftz, str, list);
        this.requestFinishedInfo.getMetricsRealTime().setDnsEndTime();
        this.requestFinishedInfo.getMetricsTime().setDnsEndTime();
        this.requestFinishedInfo.getMetrics().setDnsCache(DNManager.getInstance().getDnsCache(this.requestFinishedInfo.getHost()));
        recordEventLog("dnsEnd", this.requestFinishedInfo.getMetricsRealTime().getDnsEndTime());
    }

    public void connectStart(ftz o_ftz, InetSocketAddress inetSocketAddress, Proxy proxy) {
        super.connectStart(o_ftz, inetSocketAddress, proxy);
        EditableMetrics metrics = this.requestFinishedInfo.getMetrics();
        int i = this.retryTime;
        this.retryTime = i + 1;
        metrics.setConnectRetryTime(i);
        getIpFromInetSocketAddress(inetSocketAddress, false);
        if (this.requestFinishedInfo.getMetricsRealTime().getConnectStartTime() == 0) {
            this.requestFinishedInfo.getMetricsRealTime().setConnectStartTime();
            this.requestFinishedInfo.getMetricsTime().setConnectStartTime();
        }
        recordEventLog("connectStart", this.requestFinishedInfo.getMetricsRealTime().getConnectStartTime());
    }

    public void secureConnectStart(ftz o_ftz) {
        super.secureConnectStart(o_ftz);
        this.requestFinishedInfo.getMetricsRealTime().setSecureConnectStartTime();
        this.requestFinishedInfo.getMetricsTime().setSecureConnectStartTime();
        recordEventLog("secureConnectStart", this.requestFinishedInfo.getMetricsRealTime().getSecureConnectStartTime());
    }

    public void secureConnectEnd(ftz o_ftz, @Nullable fuk o_fuk) {
        super.secureConnectEnd(o_ftz, o_fuk);
        this.requestFinishedInfo.getMetricsRealTime().setSecureConnectEndTime();
        this.requestFinishedInfo.getMetricsTime().setSecureConnectEndTime();
        recordEventLog("secureConnectEnd", this.requestFinishedInfo.getMetricsRealTime().getSecureConnectEndTime());
    }

    public void connectEnd(ftz o_ftz, InetSocketAddress inetSocketAddress, Proxy proxy, @Nullable fut o_fut) {
        super.connectEnd(o_ftz, inetSocketAddress, proxy, o_fut);
        if (o_fut != null) {
            this.requestFinishedInfo.getMetrics().setProtocol(o_fut.toString());
        }
        getIpFromInetSocketAddress(inetSocketAddress, true);
        this.requestFinishedInfo.getMetricsRealTime().setConnectEndTime();
        this.requestFinishedInfo.getMetricsTime().setConnectEndTime();
        recordEventLog("connectEnd", this.requestFinishedInfo.getMetricsRealTime().getConnectEndTime());
    }

    public void connectFailed(ftz o_ftz, InetSocketAddress inetSocketAddress, Proxy proxy, @Nullable fut o_fut, IOException iOException) {
        super.connectFailed(o_ftz, inetSocketAddress, proxy, o_fut, iOException);
        if (o_fut != null) {
            this.requestFinishedInfo.getMetrics().setProtocol(o_fut.toString());
        }
        this.requestFinishedInfo.getMetricsRealTime().setConnectEndTime();
        this.requestFinishedInfo.getMetricsTime().setConnectEndTime();
        recordEventLog("connectFailed", this.requestFinishedInfo.getMetricsRealTime().getConnectEndTime());
    }

    public void connectionAcquired(ftz o_ftz, fuh o_fuh) {
        super.connectionAcquired(o_ftz, o_fuh);
        RealConnection realConnection = (RealConnection) o_fuh;
        this.requestFinishedInfo.getMetricsRealTime().setConnectionAcquiredTime();
        this.requestFinishedInfo.getMetricsTime().setConnectionAcquiredTime();
        recordEventLog("connectionAcquired", this.requestFinishedInfo.getMetricsRealTime().getConnectionAcquiredTime());
        if (realConnection != null) {
            this.connectionInfo = new ConnectionInfo(this.requestFinishedInfo.getHost(), realConnection);
            Route route = realConnection.route();
            fut protocol = realConnection.protocol();
            if (protocol != null) {
                this.requestFinishedInfo.getMetrics().setProtocol(protocol.toString());
            }
            if (route != null) {
                getIpFromInetSocketAddress(route.socketAddress(), true);
            }
        }
    }

    public void connectionReleased(ftz o_ftz, fuh o_fuh) {
        super.connectionReleased(o_ftz, o_fuh);
        this.requestFinishedInfo.getMetricsRealTime().setConnectionReleasedTime();
        this.requestFinishedInfo.getMetricsTime().setConnectionReleasedTime();
        recordEventLog("connectionReleased", this.requestFinishedInfo.getMetricsRealTime().getConnectionReleasedTime());
    }

    public void requestHeadersStart(ftz o_ftz) {
        super.requestHeadersStart(o_ftz);
        this.requestFinishedInfo.getMetricsRealTime().setRequestHeadersStartTime();
        this.requestFinishedInfo.getMetricsTime().setRequestHeadersStartTime();
        recordEventLog("requestHeadersStart", this.requestFinishedInfo.getMetricsRealTime().getRequestHeadersStartTime());
    }

    public void requestHeadersEnd(ftz o_ftz, fus o_fus) {
        super.requestHeadersEnd(o_ftz, o_fus);
        this.requestFinishedInfo.getMetricsRealTime().setRequestHeadersEndTime();
        this.requestFinishedInfo.getMetricsTime().setRequestHeadersEndTime();
        recordEventLog("requestHeadersEnd", this.requestFinishedInfo.getMetricsRealTime().getRequestHeadersEndTime());
    }

    public void requestBodyStart(ftz o_ftz) {
        super.requestBodyStart(o_ftz);
        this.requestFinishedInfo.getMetricsRealTime().setRequestBodyStartTime();
        this.requestFinishedInfo.getMetricsTime().setRequestBodyStartTime();
        recordEventLog("requestBodyStart", this.requestFinishedInfo.getMetricsRealTime().getRequestBodyStartTime());
    }

    public void requestBodyEnd(ftz o_ftz, long j) {
        super.requestBodyEnd(o_ftz, j);
        this.requestFinishedInfo.getMetrics().setRequestByteCount(j);
        this.requestFinishedInfo.getMetricsRealTime().setRequestBodyEndTime();
        this.requestFinishedInfo.getMetricsTime().setRequestBodyEndTime();
        recordEventLog("requestBodyEnd", this.requestFinishedInfo.getMetricsRealTime().getRequestBodyEndTime());
    }

    public void responseHeadersStart(ftz o_ftz) {
        super.responseHeadersStart(o_ftz);
        this.requestFinishedInfo.getMetricsRealTime().setResponseHeadersStartTime();
        this.requestFinishedInfo.getMetricsTime().setResponseHeadersStartTime();
        recordEventLog("responseHeadersStart", this.requestFinishedInfo.getMetricsRealTime().getResponseHeadersStartTime());
    }

    public void responseHeadersEnd(ftz o_ftz, fvb o_fvb) {
        super.responseHeadersEnd(o_ftz, o_fvb);
        this.requestFinishedInfo.getMetricsRealTime().setResponseHeadersEndTime();
        this.requestFinishedInfo.getMetricsRealTime().setTtfb(this.requestFinishedInfo.getMetricsRealTime().getResponseHeadersEndTime());
        this.requestFinishedInfo.getMetricsTime().setTtfb(o_fvb.ˏॱ());
        this.requestFinishedInfo.getMetricsTime().setResponseHeadersEndTime();
        recordEventLog("responseHeadersEnd", this.requestFinishedInfo.getMetricsRealTime().getResponseHeadersEndTime());
    }

    public void responseBodyStart(ftz o_ftz) {
        super.responseBodyStart(o_ftz);
        this.requestFinishedInfo.getMetricsRealTime().setResponseBodyStartTime();
        this.requestFinishedInfo.getMetricsTime().setResponseBodyStartTime();
        recordEventLog("responseBodyStart", this.requestFinishedInfo.getMetricsRealTime().getResponseBodyStartTime());
    }

    public void responseBodyEnd(ftz o_ftz, long j) {
        super.responseBodyEnd(o_ftz, j);
        this.requestFinishedInfo.getMetricsRealTime().setResponseBodyEndTime();
        this.requestFinishedInfo.getMetricsTime().setResponseBodyEndTime();
        recordEventLog("responseBodyEnd", this.requestFinishedInfo.getMetricsRealTime().getResponseBodyEndTime());
    }

    public void callEnd(ftz o_ftz) {
        super.callEnd(o_ftz);
        this.requestFinishedInfo.getMetricsRealTime().setCallEndTime();
        this.requestFinishedInfo.getMetricsTime().setCallEndTime();
        recordEventLog("callEnd", this.requestFinishedInfo.getMetricsRealTime().getCallEndTime());
    }

    public void callFailed(ftz o_ftz, IOException iOException) {
        super.callFailed(o_ftz, iOException);
        this.requestFinishedInfo.setException(iOException);
        this.requestFinishedInfo.getMetricsRealTime().setCallEndTime();
        this.requestFinishedInfo.getMetricsTime().setCallEndTime();
        recordEventLog("callFailed", this.requestFinishedInfo.getMetricsRealTime().getCallEndTime());
    }

    private void getIpFromInetSocketAddress(InetSocketAddress inetSocketAddress, boolean z) {
        if (inetSocketAddress != null) {
            InetAddress address = inetSocketAddress.getAddress();
            if (address == null) {
                return;
            }
            if (z) {
                this.requestFinishedInfo.getMetrics().setSuccessIp(address.getHostAddress());
            } else {
                this.requestFinishedInfo.getMetrics().addConnectIps(address.getHostAddress());
            }
        }
    }

    public RequestFinishedInfo getRequestFinishedInfo() {
        return this.requestFinishedInfo;
    }

    public ConnectionInfo getConnectionInfo() {
        return this.connectionInfo;
    }
}
